package cn.com.controller.frontController;


import cn.com.Enum.SessionAttr;
import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.*;
import cn.com.db.service.DiyCartItemService;
import cn.com.db.service.DiyCartService;
import cn.com.db.service.DiyWorkService;
import cn.com.util.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Auth(UserAuth.USER)
@RestController
@RequestMapping("front/diyCart/")
public class DiyCartController {
    Logger logger=Logger.getLogger(DiyCartController.class);
    @Autowired
    DiyCartService diyCartService;
    @Autowired
    DiyWorkService diyWorkService;
    @Autowired
    DiyCartItemService diyCartItemService;

    @RequestMapping(path = "put",method = RequestMethod.POST)
    public String put(HttpServletRequest request,
                      @RequestParam("storeId")String storeId,
                      @RequestParam("diyWorkId")String diyWorkId,
                      @RequestParam("count")Integer count)
    {
        DataResult dataResult=new DataResult();
        String userId=(String) SessionUtil.getAttr(request,SessionAttr.USERID);
        DiyCart diyCart=diyCartService.getUserDiyCart(userId);
        if (diyCart==null){
            diyCart=new DiyCart();
            diyCart.setId(UidUtil.createUUID());
            diyCart.setUser(userId);
            diyCart.setCreated_at(TimeUtil.getNowTimestamp());
            diyCartService.createDiyCart(diyCart);
        }
        DiyCartItem item=new DiyCartItem();
        item.setId(UidUtil.createUUID());
        item.setCart(diyCart.getId());
        item.setDiyWork(diyWorkId);
        item.setCount(count);
        item.setCreated_at(TimeUtil.getNowTimestamp());
        item.setUpdated_at(TimeUtil.getNowTimestamp());
        diyCartService.putItem(item);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String updateDiyCart(HttpServletRequest request,
                                @RequestParam("itemId")String itemId,
                                @RequestParam("count")Integer count)
    {
        DataResult dataResult=new DataResult();
        String userId=(String)SessionUtil.getAttr(request, SessionAttr.USERID);
        DiyCartItem item=diyCartItemService.getCartItemById(itemId);
        if (item==null){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("编辑的diy购物车条目不存在");
            logger.warn("用户:"+userId+"请求编辑了不存在的diy购物车条目("+itemId+")");
            return JsonUtil.toJsonString(dataResult);
        }
        DiyCart cart=diyCartService.getUserDiyCart(item.getCart());
        if (cart==null||!cart.getUser().equals(userId)){
            dataResult.setCode(Code.FORBIDDEN);
            dataResult.setMessage("所编辑diy购物车条目不属于当前用户,编辑失败");
            logger.warn("用户:"+userId+"请求编辑了不属于自己的购物车条目("+itemId+")");
            return JsonUtil.toJsonString(dataResult);
        }
        if (count==0){
            diyCartItemService.delete(itemId);
        }else {
            item.setCount(count);
            diyCartItemService.updateDiyCartItem(item);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("请求成功");
        return JsonUtil.toJsonString(dataResult);
    }
}
